import Vue from 'vue'
import Router from 'vue-router'
import HomePage from '@/view/HomePage'
import Login from '@/view/Login'
import Apply from '@/view/Apply'
import News from '@/view/News'
import Subject from '@/view/Subject'
import Authorization from '@/view/Authorization'
import Preview from '@/view/Preview'
import Score from '@/view/Score'
import Pay from '@/view/Pay'
import History from '@/view/History'
import PaySuccess from '@/view/PaySuccess'
import Certificate from '@/view/Certificate'
import store from '../store'

Vue.use(Router)
/**
 * 路由
 * @type {VueRouter}
 */
const router = new Router({
  /**去除路径上的# */
  // mode: 'history', 
  routes: [
    {
      path: '/',
      name: 'HomePage',
      component: HomePage
    },
    {
      path: '/Login',
      name: 'Login',
      component: Login
    },
    {
      path: '/Apply',
      name: 'Apply',
      component: Apply
    },
    {
      path: '/News/:id',
      name: 'News',
      component: News
    },
    {
      path: '/Subject',
      name: 'Subject',
      component: Subject
    },
    {
      path: '/Authorization',
      name: 'Authorization',
      component: Authorization
    },
    {
      path: '/Preview',
      name: 'Preview',
      component: Preview
    },
    {
      path: '/Score',
      name: 'Score',
      component: Score
    },
    {
      path: '/Pay',
      name: 'Pay',
      component: Pay,
      beforeEnter:(to, from, next) => {
        from.path == '/PaySuccess' ? next('/') :next();
      }
    },
    {
      path: '/History',
      name: 'History',
      component: History
    },
    {
      path: '/PaySuccess',
      name: 'PaySuccess',
      component: PaySuccess,
    },
    {
      path: '/Certificate',
      name: 'Certificate',
      component: Certificate,
    },
  ]
})

/**
 * 路由钩子设定
 * @type {string[]}
 */
// 不重定向白名单
const whites = ['/']
// 切换页面时,显示正在加载
router.beforeEach(function (to, from, next) {
  store.commit('SWITCH_IS_LOADING', true)
  if (to.path.indexOf(whites) !== -1) {
    next()
  } else {
    // 拦截操作
    next()
  }
})

// 切换页面成功后,隐藏正在加载
router.afterEach(function (to) {
  window.scrollTo(0,0)
  store.commit('SWITCH_IS_LOADING', false)
})

export default router
